/* $RCSFile: HtmlModule.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/HTMLModule/HtmlModule.ascx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:21  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:54  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Settings;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.UI.DataTypes;
using AddonNice.Configuration;
using AddonNice.Configuration.Settings;
using AddonNice.Diagnostics;
using AddonNice.Settings.PageModules.HtmlModule;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.HtmlModule
{
    public class DocStr
    {
        public const string HTMLDOCUMENT_TODO_ADDCONTENT    =   "HTMLDOCUMENT_TODO_ADDCONTENT";
    }

    /// <summary>
    /// Initializer class for HtmlModule 
    /// </summary>
    public class HtmlModuleSI : PortalModuleControlSI
    {
        public HtmlModuleSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]HtmlModuleSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]HtmlModuleSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]HtmlModuleSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);

            string extgrp           =   pm.ExtendedResourcesGroup;

			#region Module Special Settings
			SettingItemGroup _Group     = SettingItemGroup.MODULE_SPECIAL_SETTINGS;
			// Add editor to base settings
			HtmlEditorDataType.AddEditorToSettings(bSettings, _Group,extgrp);
			#endregion
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]HtmlModuleSI InitSettings END.",ModuleTraceSwitch.Sw.Info);
        }
    }

    public partial class HtmlModule : PortalModuleControl 
    {
				
		public override Guid GuidID 
		{
			get
			{
				return new Guid("{0B113F51-FEA3-499A-98E7-7B83C192FDBB}");
			}
		}
		
        /// <summary>
        /// Ctor
        /// </summary>
		public HtmlModule()
		{
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]HtmlModule Ctor Called.",ModuleTraceSwitch.Sw.Info);
		}

        public override string ExtendedResourcesGroup 
		{
            get
            {
                const string extgrp         =   "HTMLMODULE";
                return extgrp;
            }
		}

        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new HtmlModuleSI(bSettings,this);
        }

        /// <summary>
        /// The Page_Load event handler on this User Control is
        /// used to render a block of HTML or text to the page.  
        /// The text/HTML to render is stored in the HtmlText 
        /// database table.  This method uses the AddonNice.HtmlTextDB()
        /// data component to encapsulate all data functionality.
        /// </summary>
        protected void Page_Load(object sender, System.EventArgs e) 
        {
            // Obtain the selected item from the HtmlText table
            HtmlModuleDB ModuleDB	=	new HtmlModuleDB ();
			SqlDataReader dr		=	null;
			try
			{
				dr					=	ModuleDB.GetHtmlTextGlobalized(ModuleID);

				if (dr.Read())
				{
					Content	        =   Server.HtmlDecode((String) dr["DesktopHtml"]);
					HtmlHolder.Controls.Add(new LiteralControl(Content.ToString()));
				}
			}
			catch(Exception ex)
			{
				if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HTMLModule Page_Load  ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
                throw;
			}
			finally
			{
				if ( dr != null )
					dr.Close();
			}

        }
 		
		#region Std overrides

        /// <summary>
        /// Triggered from Page.OnPreInit
        /// We must set the Add button before buttons are initialized by base method
        /// </summary>
        public override void Page_PreInit(object sender, EventArgs e)
        {
			this.EditUrl = Config.ModulesPath+"/HTMLModule/HtmlEdit.aspx";
			this.EditText	=	"EDIT";
            base.Page_PreInit(sender, e);
        }

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent() 
        {
			Load	+=  new EventHandler(Page_Load);
		}
		#endregion
    }
}
